<?php
/**
 *Author: Nguyễn Mạnh Hùng
 *Created date: 19-04-2012
 */

class QuestionsController extends AppController {
	
	public $uses = array('QuestionType', 'QuestionLevel', 'QuestionGroup', 'Question', 'Answer');
	
	public $layout = 'default_account';
	
	public function beforeFilter() {
		parent::beforeFilter();
	}
	
	public function index() {
		$this->Paginator->settings = array(
				'fields' => array(
						'Question.id', 
						'Question.parent_id', 
						'Question.content', 
						'Question.audio_url',
						'Question.active',
						'Question.created',  
						'ParentQuestion.id', 
						'ParentQuestion.Content',
						'ParentQuestion.audio_url',
						'QuestionType.name',
						'QuestionLevel.name',
						'User.first_name' , 
						'User.last_name'), 
				'conditions' => array(
					'Question.parent_id' => ''),
				'limit' => LIMIT_NUMBER, 
				'order' => 'Question.id desc')
		;
		$questions = $this->paginate('Question');
		$this->set('questions', $questions);
		//debug($questions);
	}
	
	public function add(){
		if (!empty($this->data)) {
			$data = $this->request->data;
			$create = $this->CreateQuestion($data);
			if ($create) {
				$this->Session->setFlash(__('The question is successfully added'));
				$this->redirect('/allQuestions');
			}
		}
		$QuestionTypes = $this->QuestionType->find('all', array('recursive' => 0));
		$QuestionLevels = $this->QuestionLevel->find('all', array('recursive' => 0));
		$arrayTypes = null;
		$arrayTypes[0] = 'Select';
		foreach ($QuestionTypes as $questionType){
			$arrayTypes[$questionType['QuestionType']['id']] = $questionType['QuestionType']['name'];
		}
		$arrayLevels = NULL;
		$arrayLevels[0] = 'Select';
		foreach ($QuestionLevels as $questionLevel){
			$arrayLevels[$questionLevel['QuestionLevel']['id']] = $questionLevel['QuestionLevel']['name'];
		}
		$this->set('QuestionTypes', $arrayTypes);
		$this->set('QuestionLevels', $arrayLevels);
	}
	
	public function delete($id=null){
		if(!$id){
			$this->Session->setFlash(__('The question is not invalid'));
				$this->redirect('/allQuestions');
		}
		else{			
			$questions = $this->Question->find('all', array('conditions'=>array('Question.parent_id'=>$id)));
			foreach($questions as $question){
				$this->Answer->deleteAll(array('question_id'=>$question['Question']['id']));
			}
			$this->Answer->deleteAll(array('question_id'=>$id));
			
			$this->Question->deleteAll(array('Question.parent_id'=>$id));
			if($this->Question->delete($id)){
				$this->Session->setFlash(__('The question is successfully deleted'));
				$this->redirect('/allQuestions');
			}
		}
	}
	
	public function CreateQuestion($data){
		$this->Question->set($data);
		if ($this->Question->AddQuestionValidate()) {
			$user = $this->UserAuth->getUser();
			$data['Question']['user_id'] = $user['User']['id'];
			$data['Question']['created'] = date('Y-m-d H:i:s', time());
			$data['Question']['modified'] =date('Y-m-d H:i:s', time());
			if (!empty($data['Question']['audio_url'])) {
				$file = base64_decode($data['Question']['audio_url']);
				$data['Question']['audio_url'] = $file; 
			}
			$this->Question->save($data,false);
			return true;
		}
		return false;
	}
	
	public function edit($id){
		if (!empty($this->data)) {
			if($this->Question->save($this->data)){
				$this->Session->setFlash(__('The question is successfully edited'));
				$this->redirect('/allQuestions');
			}
		}
		$Question = $this->Question->find('first', array('recursive'=>-1, 'conditions'=>array('id'=>$id)));
		$QuestionTypes = $this->QuestionType->find('all', array('recursive' => 0));
		$QuestionLevels = $this->QuestionLevel->find('all', array('recursive' => 0));
		$arrayTypes = null;
		$arrayTypes[0] = 'Select';
		foreach ($QuestionTypes as $questionType){
			$arrayTypes[$questionType['QuestionType']['id']] = $questionType['QuestionType']['name'];
		}
		$arrayLevels = NULL;
		$arrayLevels[0] = 'Select';
		foreach ($QuestionLevels as $questionLevel){
			$arrayLevels[$questionLevel['QuestionLevel']['id']] = $questionLevel['QuestionLevel']['name'];
		}
		$this->set('Question', $Question);
		$this->set('QuestionTypes', $arrayTypes);
		$this->set('QuestionLevels', $arrayLevels);
		$this->set('id', $id);
	}
	
	public function insert(){
		$this->autoRender = false;
		/*$question = $this->data['Question'];
		
		//if parent_id doesn't exist then remove it from array.
		if($question['parent_id'] == 0)
			unset($question['parent_id']);
		
		$this->Question->save($question);*/
		debug($this);
	}
	
	public function show($questionId, $position = null){
		if (!empty($questionId)) {
			$question = $this->Question->read(null, $questionId);
			$this->set('question', $question);
			$this->set('position', $position);
			//debug($question);
		} else {
			$this->redirect('/allQuestions');
		}
	}
	
	public function DialogAddQuestion($parent_id){
		$QuestionTypes = $this->QuestionType->find('all', array('recursive' => 0));
		$QuestionLevels = $this->QuestionLevel->find('all', array('recursive' => 0));
		$arrayTypes = null;
		$arrayTypes[0] = 'Select';
		foreach ($QuestionTypes as $questionType){
			$arrayTypes[$questionType['QuestionType']['id']] = $questionType['QuestionType']['name'];
		}
		$arrayLevels = NULL;
		$arrayLevels[0] = 'Select';
		foreach ($QuestionLevels as $questionLevel){
			$arrayLevels[$questionLevel['QuestionLevel']['id']] = $questionLevel['QuestionLevel']['name'];
		}
		
		$count= $this->Question->find('count', array('conditions' => array('Question.parent_id' => $parent_id)));
		$position = $count + 1;
		$this->set('QuestionTypes', $arrayTypes);
		$this->set('QuestionLevels', $arrayLevels);
		$this->set('parent_id', $parent_id);
		$this->set('position', $position);
	}
	
	public function AddQuestionByJson() {
		$query = $this->request->query;
		$data['Question']['parent_id'] = $query['parent_id'];
		$data['Question']['question_type_id'] = $query['question_type_id'];
		$data['Question']['question_level_id'] = $query['question_level_id'];
		$data['Question']['content'] = $query['content'];
		$create = $this->CreateQuestion($data);
		$question_id = $this->Question->getLastInsertID();
		if ($create) {
			$this->set('status', true);
			$this->set('message', 'Add question success');
		}else {
			$this->set('status', false);
			$this->set('message', 'Please enter input');
		}
		$this->set('questionId', $question_id);
		$this->set('_serialize', array('status', 'message', 'questionId'));
	}
	
	public function ShowQuestionByJson(){
		$id = $this->request->query['question_id'];
		$questioin = $this->Question->find('first', array(
					'fields' => array(
						'Question.id',
						'Question.content', 
						'Question.audio_url', 
					),
					'conditions' => array('Question.id' => $id),
					'recursive' => 0
				));
		$this->set('question', $questioin);
		$this->set('_serialize', array('question'));
	}
	
	public function ShowQuestion($question_id){
		$this->set('question_id', $question_id);
	}
	
}

?>